********************************************************************************
* Purpose: Creates somatic hospitalization outcomes for mother and child from 6 to 24 months after birth
********************************************************************************

do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 


***************************************************************
* appending raw data
**************************************************************

forvalues i = 2012/2018 {
use "$raw\LPRADM`i'",clear
keep pnr D_IND D_UD V_BEH V_SENGD
compress
save "$work\Hospital`i'", replace	
	
}
forvalues i = 2012/2018 {
use "$work\Hospital`i'",clear
merge m:1 pnr using "$work\mfr_kids", keep(3) nogen
save "$work\Hospital`i'_merged_pnr", replace	
}
use "$work\Hospital2012_merged_pnr",clear
forvalues i = 2013/2018 {
append using "$work\Hospital`i'_merged_pnr"
}
save "$work\Hospkids",replace	


* mothers hosp contacts
forvalues i = 2012/2018 {
forvalues j = 1/5 {
use "$work\moms_no`j'",clear
rename pnr pnrb
rename id_mother pnr
merge 1:m pnr using "$work\Hospital`i'", keep(3) nogen
save "$work\Hospital`i'merged_cprm`j'",replace
}	
}
forvalues i = 2012/2018 {
use "$work\Hospital`i'merged_cprm1"
forvalues j = 2/5 {
append using "$work\Hospital`i'merged_cprm`j'"
}
save "$work\Hospital`i'merged_cprm", replace
}
use "$work\Hospital2012merged_cprm",clear
forvalues i = 2013/2018 {
append using "$work\Hospital`i'merged_cprm", force
}
save "$work\Hospital_cprm", replace


*******************************************************
* Hospitalizations kids
*******************************************************
use "$work\Hospkids",clear
duplicates drop
gen duringbirth = .
replace duringbirth = 1 if D_INDDTO == mfr_dob
drop if duringbirth == 1
replace duringbirth = 1 if D_INDDTO == mfr_dob +1
sort pnr D_INDDTO
replace duringbirth = 1 if duringbirth[_n-1]== 1 & D_INDDTO<=D_INDDTO[_n-1] & pnr[_n+1]==pnr
drop if duringbirth == 1
drop duringbirth

sort D_INDDTO D_UDDTO
gen drop =.
replace drop = 1 if D_INDDTO[_n+1]==D_INDDTO & D_UDDTO[_n+1]>=D_UDDTO & pnr[_n+1]==pnr
replace drop = 1 if D_INDDTO[_n+1]>D_INDDTO & D_UDDTO[_n+1]<D_UDDTO & pnr[_n+1]==pnr
drop if drop == 1

sort pnr D_UDDTO D_INDDTO
by pnr: replace drop = 1 if D_UDDTO==D_UDDTO[_n-1] & D_INDDTO>D_INDDTO[_n-1]
drop if drop == 1

sort pnr D_INDDTO D_UDDTO
bys pnr: gen help = 1 if D_UDDTO > D_INDDTO[_n+1] & pnr[_n+1]==pnr
bys pnr: egen help1=min(help)
by pnr: replace D_UDDTO=D_UDDTO[_n+1] if D_INDDTO<D_INDDTO[_n+1] & D_UDDTO<D_UDDTO[_n+1] & pnr==pnr[_n+1] & help==1

replace V_SENGD=D_UDDTO-D_INDDTO if help==1
bys pnr D_UDDTO: egen min=min(D_INDDTO)
keep if D_INDDTO==min
drop min help*
drop if D_INDDTO-mfr_dob<182 // exclude first 6mo
gen age_at = floor((D_INDDTO-mfr_dob)/365.25)
gen hospage0 = (age_at==0)
gen hospage1 = (age_at==1)
gen hospage2 = (age_at==2)

collapse (max) hospage* , by(pnr)
compress 
save "$work\hospitalizationskids", replace

*******************************************************
* Hospitalizations moms
*******************************************************

use "$work\Hospital_cprm",clear
duplicates drop
drop if D_INDDTO-mfr_dob<182
gen age_at = floor((D_INDDTO-mfr_dob)/365.25)
gen hospagem0 = (age_at==0)
gen hospagem1 = (age_at==1)
gen hospagem2 = (age_at==2)

collapse (max) hospage* , by(pnr mfr_dob)
rename pnr id_mother
compress 
save "$work\hospitalizationsmoms", replace